//
//  UserBestListCell.m
//  CoolTennisBall
//
//  Created by Coollang on 16/9/9.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserBestListCell.h"
#import "Masonry.h"
#import "UserDataModel.h"
#import "UserBestDataView.h"

@interface UserBestListCell()
@property (weak, nonatomic) IBOutlet UIView *dotView;
@property (weak, nonatomic) IBOutlet UIView *bottomLineView;
@property (weak, nonatomic) IBOutlet UIView *topLineView;
@property (weak, nonatomic) IBOutlet UILabel *bestTimelabel;
@property (weak, nonatomic) IBOutlet UIView *otherSuperView;
@property (weak, nonatomic) IBOutlet UIView *oneOtherView;
@property (weak, nonatomic) IBOutlet UIView *twoOtherView;
@property (weak, nonatomic) IBOutlet UIView *threeOtherView;
@property (weak, nonatomic) IBOutlet UIView *normalView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *oneOtherHeightCon;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *TwoOtherHeightCon;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *threeOtherHeightCon;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *spaceWidthCon;

@property (nonatomic, strong) UserBestDataView *normMalBestView;
@property (nonatomic, strong) UserBestDataView *oneBestView;
@property (nonatomic, strong) UserBestDataView *twoBestView;
@property (nonatomic, strong) UserBestDataView *threeBestView;

@property (nonatomic, strong) CAGradientLayer *gradientLayer;

@property (nonatomic, assign)BOOL isShowGraiendtLayer;

@property (weak, nonatomic) IBOutlet UIView *sectionSepBottomLine;
@property (weak, nonatomic) IBOutlet UIView *sectionSepTopLine;

@end

@implementation UserBestListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    YFLanguage curentLanguage = [NSBundle currentLangeuage];
    if (kYFScreenWidth < 375) {
        if (curentLanguage == kLanguageChineseSimple) {
             self.spaceWidthCon.constant = 30;
        }else {
             self.spaceWidthCon.constant = 20;
        }
    }else {
        if (curentLanguage == kLanguageChineseSimple) {
            self.spaceWidthCon.constant = kAutoWid(55);
        }else if (curentLanguage == kLanguageFrennch){
            self.spaceWidthCon.constant = kAutoWid(35);
        }else {
            self.spaceWidthCon.constant = kAutoWid(50);
        }
    }
    
    self.dotView.layer.cornerRadius = self.dotView.bounds.size.width * 0.5;
    self.dotView.layer.masksToBounds = YES;
    
    self.dotView.layer.borderColor = kLineBackgroundColor.CGColor;
    self.dotView.layer.borderWidth = 1;
    
}
- (void)layoutSubviews {
    [super layoutSubviews];
    //最后一个cell创建一个渐变了layer
    [self CreatGradientLayer];
    
}
- (void)CreatGradientLayer {
    
    if (self.gradientLayer) {
        [self.gradientLayer removeFromSuperlayer];
    }
    // 创建一个渐变色Layer
    [self.bottomLineView layoutIfNeeded];
    
    CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
    gradientLayer.frame = self.bottomLineView.bounds;
    
    gradientLayer.colors = @[(__bridge id)[UIColor lightGrayColor].CGColor, (__bridge id)[UIColor whiteColor].CGColor];
    
    [gradientLayer setStartPoint:CGPointMake(0.5, 0)];
    [gradientLayer setEndPoint:CGPointMake(0.5, 1)];
    [self.bottomLineView.layer addSublayer:gradientLayer];
    self.gradientLayer = gradientLayer;
    self.gradientLayer.hidden = !self.isShowGraiendtLayer;
    
    if (self.isShowGraiendtLayer) {
        self.bottomLineView.hidden = NO;
    }
    
}

- (void)setBestListModel:(UserBestListModel *)bestListModel {
    _bestListModel = bestListModel;
    self.bestTimelabel.text = bestListModel.bestTime;
    if (self.bestListModel.bestDataList.count <= 1) {
        self.oneOtherHeightCon.constant = 0;
        self.TwoOtherHeightCon.constant = 0;
        self.threeOtherHeightCon.constant = 0;
        [self.normMalBestView configerWithBestModel:bestListModel.bestDataList[0]];
    }else if (self.bestListModel.bestDataList.count == 2){
        self.oneOtherHeightCon.constant = 45;
        self.TwoOtherHeightCon.constant = 0;
        self.threeOtherHeightCon.constant = 0;
        [self.normMalBestView configerWithBestModel:bestListModel.bestDataList[0]];
        [self.oneBestView configerWithBestModel:bestListModel.bestDataList[1]];
    }else if (self.bestListModel.bestDataList.count == 3){
        self.oneOtherHeightCon.constant = 45;
        self.TwoOtherHeightCon.constant = 45;
        self.threeOtherHeightCon.constant = 0;
        [self.normMalBestView configerWithBestModel:bestListModel.bestDataList[0]];
        [self.oneBestView configerWithBestModel:bestListModel.bestDataList[1]];
        [self.twoBestView configerWithBestModel:bestListModel.bestDataList[2]];

    }else if (self.bestListModel.bestDataList.count == 4){
        self.oneOtherHeightCon.constant = 45;
        self.TwoOtherHeightCon.constant = 45;
        self.threeOtherHeightCon.constant = 45;
        [self.normMalBestView configerWithBestModel:bestListModel.bestDataList[0]];
        [self.oneBestView configerWithBestModel:bestListModel.bestDataList[1]];
        [self.twoBestView configerWithBestModel:bestListModel.bestDataList[2]];
        [self.threeBestView configerWithBestModel:bestListModel.bestDataList[3]];
    }
    
}

- (void)isShowBottomLine:(BOOL)isShow {
    self.bottomLineView.hidden = !isShow;
    self.sectionSepBottomLine.hidden = isShow;
}

- (void)isShowTopLine:(BOOL)isShow {
    self.topLineView.hidden = !isShow;
    self.sectionSepTopLine.hidden = isShow;
}

- (void)isShowGradientLayer:(BOOL)isShow {
    self.isShowGraiendtLayer = isShow;
}

#pragma mark - 懒加载
- (UserBestDataView *)normMalBestView {
    if (_normMalBestView == nil) {
        _normMalBestView = [UserBestDataView userBestDataView];
        [self.normalView addSubview:_normMalBestView];
        [_normMalBestView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.normalView);
        }];
    }
    return _normMalBestView;
}

- (UserBestDataView *)oneBestView {
    if (_oneBestView == nil) {
        _oneBestView = [UserBestDataView userBestDataView];
        [self.oneOtherView addSubview:_oneBestView];
        [_oneBestView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.oneOtherView).offset(18);
            make.bottom.left.right.equalTo(self.oneOtherView);
        }];
    }
    return _oneBestView;
}
- (UserBestDataView *)twoBestView {
    if (_twoBestView == nil) {
        _twoBestView = [UserBestDataView userBestDataView];
        [self.twoOtherView addSubview:_twoBestView];
        [_twoBestView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.twoOtherView).offset(18);
            make.bottom.left.right.equalTo(self.twoOtherView);
        }];
    }
    return _twoBestView;
}

- (UserBestDataView *)threeBestView {
    if (_threeBestView == nil) {
        _threeBestView = [UserBestDataView userBestDataView];
        [self.threeOtherView addSubview:_threeBestView];
        [_threeBestView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.threeOtherView).offset(18);
            make.bottom.left.right.equalTo(self.threeOtherView);
        }];
    }
    return _threeBestView;
}

@end
